package com.nuc.thread.status;

import lombok.extern.slf4j.Slf4j;

/**
 * @Description 测试Java线程的六种状态
 * @Author 诗酒趁年华
 * @Version V1.0.0
 * @Date 2021/5/9
 */
@Slf4j(topic = "TestState")
public class TestState {
    public static void main(String[] args) {
        Thread t1 = new Thread("t1"){
            @Override
            public void run() {
                log.debug("t1 running");
            }
        };

        Thread t2 = new Thread("t2"){
            @Override
            public void run() {
                log.debug("t2 running");
            }
        };
        t2.start();

        Thread t3 = new Thread("t3"){
            @Override
            public void run() {
                while (true){

                }
            }
        };
        t3.start();

        Thread t4 = new Thread("t4"){
            @Override
            public void run() {
                synchronized (TestState.class){
                    try {
                        Thread.sleep(1000000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        t4.start();

        Thread t5 = new Thread("t5"){
            @Override
            public void run() {
                try {
                    t4.join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        t5.start();

        Thread t6 = new Thread("t6"){
            @Override
            public void run() {
                synchronized (TestState.class){
                    try {
                        Thread.sleep(100000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        t6.start();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.debug("t1:{}",t1.getState());//NEW 没有启动该线程
        log.debug("t2:{}",t2.getState());//TERMINATED 执行完毕
        log.debug("t3:{}",t3.getState());//RUNNABLE 正在运行的线程也是该状态
        log.debug("t4:{}",t4.getState());//TIMED_WAITING sleep方法使线程进入TIMED_WAITING状态
        log.debug("t5:{}",t5.getState());//WAITING 等待t4线程执行完
        log.debug("t6:{}",t6.getState());//BLOCKED t4线程先执行，加锁。t6线程拿不到锁 进入BLOCKED状态

    }
}
